package com.ys.data.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.common.base.BaseEnum;
import com.common.exception.BaseException;
import com.common.vo.OrderVo;
import com.ys.data.dao.OrderMapper;
import com.ys.data.service.OrderService;
import org.springframework.beans.BeanUtils;
import org.springframework.data.annotation.Transient;
import org.springframework.stereotype.Service;
import com.ys.data.entry.Order;

import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;

/**
 * @version: V1.0
 * @author: szw
 * @className: OrderServiceImpl
 * @packageName: OrderServiceImpl
 * @description: 订单接口实现
 * @data: 2021-1-22
 **/
@Service
public class OrderServiceImpl extends ServiceImpl<OrderMapper, Order> implements OrderService {

    @Resource
    private  OrderMapper orderMapper;

    /**
     * 添加订单
     * @param orderVo
     * @return int
     * @throws Exception
     */
    @Transient
    @Override
    public int save(OrderVo orderVo) throws Exception {
        if (Objects.isNull(orderVo)) {
            throw new BaseException(BaseEnum.ORDER_DATA_IS_NULL);
        }
        try {
            return saveToUpdate(orderVo, true);
        } catch (Exception e) {
            throw new BaseException(BaseEnum.SAVE_ORDER_DATA_IS_ERROR);
        }
    }

    /**
     *  修改订单
     * @param orderVo
     * @return int
     * @throws Exception
     */
    @Transient
    @Override
    public int update(OrderVo orderVo) throws Exception {
        if (Objects.isNull(orderVo)) {
            throw new BaseException(BaseEnum.ORDER_DATA_IS_NULL);
        }
        try {
            return saveToUpdate(orderVo, false);
        } catch (Exception e) {
            throw new BaseException(BaseEnum.UPDATE_ORDER_DATA_IS_ERROR);
        }
    }

    /**
     * 查询订单详情
     * @param id
     * @return
     * @throws Exception
     */
    @Override
    public Order findOrderById(Long id) throws Exception {
        if (null==id) {
            throw new BaseException(BaseEnum.ID_IS_NULL);
        }
        try {
            return orderMapper.selectById(id);
        } catch (Exception e) {
            throw new BaseException(BaseEnum.FIND_ORDER_DATA_IS_ERROR);
        }
    }

    /**
     * 根据调价查询订单
     * @param queryWrapper
     * @return
     * @throws Exception
     */
    @Transient
    @Override
    public Order findOrder(QueryWrapper<Order> queryWrapper) throws Exception {
        if (null==queryWrapper) {
            throw new BaseException(BaseEnum.FIND_PARAMETER_IS_NULL);
        }
        try {
            return orderMapper.selectOne(queryWrapper);
        } catch (Exception e) {
            throw new BaseException(BaseEnum.FIND_ORDER_DATA_IS_ERROR);
        }
    }

    /**
     * 查询用户已完成的订单
     * @param queryWrapper
     * @return
     * @throws Exception
     */
    @Override
    public List<Order> findUserOrders(QueryWrapper<Order> queryWrapper) throws Exception {
        if (null==queryWrapper) {
            throw new BaseException(BaseEnum.FIND_PARAMETER_IS_NULL);
        }
        try {
            return orderMapper.selectList(queryWrapper);
        } catch (Exception e) {
            throw new BaseException(BaseEnum.FIND_ORDER_DATA_IS_ERROR);
        }
    }

    /**
     * 删除订单
     * @param id
     * @return int
     * @throws Exception
     */
    @Transient
    @Override
    public int delete(Long id) throws Exception {
        if (null==id) {
            throw new BaseException(BaseEnum.ID_IS_NULL);
        }
        try {
            return orderMapper.deleteById(id);
        } catch (Exception e) {
            throw new BaseException(BaseEnum.DELETE_ORDER_DATA_IS_ERROR);
        }
    }

    /**
     * 分页查询订单
     * @param page
     * @param orderVo
     * @return
     * @throws Exception
     */
    @Override
    public IPage<Order> orderPage(Page<Order> page, OrderVo orderVo) throws Exception {
        if (Objects.isNull(orderVo)) {
            throw new BaseException(BaseEnum.ORDER_DATA_IS_NULL);
        }
        try {
            QueryWrapper<Order> queryWrapper = new QueryWrapper<>();
            return orderMapper.selectPage(page, queryWrapper);
        } catch (Exception e) {
            throw new BaseException(BaseEnum.FIND_ORDER_DATA_IS_ERROR);
        }
    }

    /**
     * 更新、保存订单
     *
     * @param orderVo
     * @param flag   true 保存 false 更新
     * @return
     */
    private int saveToUpdate(OrderVo orderVo, Boolean flag) {
        Order order = new Order();
        BeanUtils.copyProperties(orderVo, order);
        if (flag) {
            return orderMapper.insert(order);
        }
        return orderMapper.updateById(order);
    }
}
